import moment from "moment";

/**
 * 格式化日期
 * @param date
 * @param fmt
 * @returns
 */
export const formatDate = (originVal) => {
  return moment(originVal).format("yy-M-D");
};

/**
 * 格式化时间
 * @param date
 * @param fmt
 * @returns
 */
export const formatTime = (originVal) => {
  return moment(originVal).format("mm:ss");
};

/**
 * 对象转URL参数
 * @param param 将要转为URL参数字符串的对象
 * @param idx 循环第几次，用&拼接
 * @param key URL参数字符串的前缀
 * @param encode true/false 是否进行URL编码,默认为false
 * @returns URL参数字符串
 */
export const urlEncode = (param, idx = 1, key = null, encode = false) => {
  if (param == null) return "";
  var paramStr = "";
  var t = typeof param;
  if (t == "string" || t == "number" || t == "boolean") {
    var one_is = idx < 3 ? "?" : "&";
    paramStr +=
      one_is +
      key +
      "=" +
      (encode == null || encode ? encodeURIComponent(param) : param);
  } else {
    for (var i in param) {
      var k =
        key == null
          ? i
          : key + (param instanceof Array ? "[" + i + "]" : "." + i);
      idx++;
      paramStr += urlEncode(param[i], idx, k, encode);
    }
  }
  return paramStr;
};
